package com.baidumap.administrator.happystudy.module.search;

import android.content.Context;
import android.view.View;
import android.widget.ListView;

import com.ab.http.OnMessageResponse;
import com.ab.util.ListUtils;
import com.ab.util.ToastUtil;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.WaiMaiGoodsListAdapter;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.module.search.model.Search;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * 描述：
 * 作者：xiaoxing on 17/5/4 09:27
 * 邮箱：2235445233@qq.com
 */
public class SearchActivity extends AppBaseActivity implements OnMessageResponse {

    public static SearchActivity instance;

    public ImageLoader loader;
    private Search_View mSearchView;
    private static int currentPage = 1;
    @Bind(R.id.list)
    ListView mListView;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private WaiMaiGoodsListAdapter mAdapter;
    private ArrayList<HashMap<String, Object>> result = new ArrayList<>();

    @Override
    public int bindLayout() {
        return R.layout.activity_search;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        instance = this;
        mSearchView = (Search_View) findViewById(R.id.search_layout);
        // LoadImage初始化
        loader = ImageLoader.getInstance();
        mAdapter = new WaiMaiGoodsListAdapter(this, loader);
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setTitleHidden();

    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        mSearchView.setHistoryHidden();
        list.clear();
        mAdapter.bindData(list);
        mAdapter.notifyDataSetChanged();
        if (url.equals(Constant.PRODUCT_SEARCH) && jo != null) {
            Search search = new Search(jo.toString());

            if (search != null) {


                if (search.getCode().equals("200")) {

                    List<Search.DataBean.ProductBean> dataBeanList = search.getData().getProduct();


                    HashMap<String, Object> map = null;

                    for (int i = 0; i < dataBeanList.size(); i++) {


                        map = new HashMap<>();
                        map.put("shop_id", dataBeanList.get(i).getProduct_id());
                        map.put("shop_name", dataBeanList.get(i).getProduct_name());
                        map.put("shop_img", dataBeanList.get(i).getProduct_pic1());
//                        map.put("shop_rating", dataBeanList.get(i).getPrice());
                        map.put("shop_distance", dataBeanList.get(i).getPrice_tuangou());
                        map.put("shop_walk_time", dataBeanList.get(i).getBuy_numbers());
                        result.add(map);

                    }

                    list.addAll(result);//

                    ListUtils.removeDuplicate(list);

                    mAdapter.bindData(list);


                    if (currentPage == 1) {

                        mListView.setAdapter(mAdapter);

                    }
                    mAdapter.notifyDataSetChanged();

                    ListUtils.setListViewHeightBasedOnChildren(mListView);
                } else {
                    ToastUtil.showMessage(this, search.getMsg());
                }
            }
        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
